/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.brillien.kernel.axiom.atomic;

import com.vii.brillien.kernel.BrillienException;
import com.vii.brillien.kernel.axiom.transport.Communication;
import com.vii.brillien.kernel.axiom.transport.Mediator;

import java.util.Map;
import java.util.logging.Level;

/**
 * Instantiation manager interface, factory-like interface according the environment of the component like: used libraries, component version, transaction management, instance management type, etc.
 * A Presence type (class) is managed by this object to create/build/stop/destruct instances.
 */
public interface PresenceManager <P extends Presence, C extends Communication> extends Unit<P, C, Object> {

    /**
     * Initializes the PresenceManager with the given class instance, reads the annotations of the presence type
     * @param PresenceClass presence class instance
     */
    void                initPresenceManager( String presenceName, Class<P> PresenceClass ) throws BrillienException;

    /**
     * Activates the PresenceManager according to the initialized configuration.
     */
    void                activatePresenceManager( ) throws BrillienException;

    /**
     * Getter method to request the Class definition of a specific Presence type
     */
    Class<P>            getPresenceClass();

    /**
     * Getter method to request the prefix for generating FlowIDs
     */
    String              getFlowIDPrefix();

    /**
     * Getter method for a new instance of the Presence type specifying the caller
     */
    P                   getInstance( Presence caller ) throws BrillienException;

    /**
     * Getter method for a new instance of the Presence type specifying the ID of the flow
     */
    P                   getInstance( String flowID ) throws BrillienException ;

    /**
     * Getter method for a new instance of the Presence type specifying the caller
     */
    P                   getInstance( Presence caller, String name ) throws BrillienException;

    /**
     * Getter method for a new instance of the Presence type specifying the ID of the flow
     */
    P                   getInstance( String flowID, String name ) throws BrillienException ;

    /**
     * All Presence instances managed by this PresenceManager will be retrieved
     * @param presences presences to be retrieved
     */
    void                retrieveReferences(P... presences) throws BrillienException;

    /**
     * Getter method for the classloader assigned to this PresenceManager
     */
    ClassLoader         getClassLoader( );

    /**
     * Sets classloader to be assigned to this PresenceManager
     */
    void                setClassLoader(ClassLoader classLoader);

    /**
     * Getter method to get the instance creation method to follow regarding this Presence type.
     * One of the following will be returned:
     * RESIDENT, Sparkle, CONSONANT
     * @return instance creation type ID
     */
    int                 getManagementType();

    /**
     * Retrieves the timeUnit value of the given Presence type. Measured in milliseconds.
     */
    long                getPresenceTimeMeasure();

    /**
     * Sets the timeUnit value of the the given Presence type, read from the nnnotation of the type. Measured in milliseconds.
     */
    void                setPresenceTimeMeasure(long timeMeasure);

    /**
     * Gets the periodicity flag of the given Presence type
     */
    boolean             isPresencePeriodical();

    /**
     * Sets the periodicity flag of the given Presence type
     */
    void                setPresencePeriodical(boolean presencePeriodical);
    
    /**
     * Gets the timout for message processing. Every incoming message which has been sent before (currentTimeMillis-expiration) is considered as expired amd will be ignored.
     */
    long                getMessageExpiration();

    /**
     * Sets the timout for message processing. Every incoming message which has been sent before (currentTimeMillis-expiration) is considered as expired amd will be ignored.
     */
    void                setMessageExpiration(long messageExpiration);

    /**
     * Getter method for log type of the specific Presence type
     */
    public int          getLogType();

    /**
     * Setter method for log type of the specific Presence type
     * @param logType log type
     */
    void                setLogType(int logType);

    /**
     * Getter method for log level of the specific Presence type
     */
    Level               getLogLevel();

    /**
     * Setter method for log level of the specific Presence type
     * @param logLevel log level
     */
    void                setLogLevel(String logLevel);


    /**
     * Getter method for getting input parameters
     * @return parametersMap parameters' hashmap read from the presence config file
     */
    Map<String, Object> getParameters();

    /**
     * Setter method for adding input Parameters for every given Presence creation
     * @param parameters parameters' hashmap read from the presence config file
     */
    void                setParameters(Map<String, Object> parameters);

    /**
     * Retrieves a mediator representing the transport layer
     */
    <T> Mediator<C, T>  getMediator();

    /**
     * Sets custom SSO presence
     */
    void                setSSOPresenceName(String ssoPresenceName);


    /**
     * Presences let the PresenceManager to know about the messages sent through this method
     */
    void                markMessageToSend(C shared, C message);

}
